home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / CoreSource / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  4.1 KB  |  228 lines  |  [TEXT/KAHL]

  1. /*
  2. */
  3.  
  4. #include "CoreGlobals.h"
  5.  
  6.  
  7. Boolean        gDone;
  8. Boolean        gBackgroundFlag;
  9. EventRecord gEvent;
  10.  
  11. long GetBackTime(void);
  12.  
  13. void InitEventGlobals(void)
  14. {
  15.     gDone = false;
  16.     gBackgroundFlag = false;
  17. }
  18.  
  19. void DoCloseWindow(WindowPtr theWindow)
  20. {
  21.     if(TrackGoAway(theWindow,gEvent.where)) {
  22.         CloseGameWindow(theWindow,GetWRefCon(theWindow));
  23.     }
  24. }
  25.  
  26. void DoClickInContent(WindowPtr theWindow)
  27. {
  28.     if(theWindow!=FrontWindow()) {
  29.         SelectWindow(theWindow);
  30.     } else {
  31.         ClickGameWindow(theWindow,GetWRefCon(theWindow));
  32.     }
  33. }
  34.  
  35. void DoDragWindow(WindowPtr theWindow)
  36. {
  37.     DragWindow(theWindow,gEvent.where,&screenBits.bounds);
  38. }
  39.  
  40.  
  41. void DoGrowWindow(WindowPtr theWindow)
  42. {
  43.     long    newSize;
  44.     int        newHeight,newWidth;
  45.     Rect    growLimitSizes;
  46.     
  47.     SetPort(theWindow);
  48.     InvalRect(&theWindow->portRect);
  49.     
  50.     growLimitSizes.top = 40;            /* min height */
  51.     growLimitSizes.bottom = 32767;        /* max height */
  52.     growLimitSizes.left = 40;             /* min width */
  53.     growLimitSizes.right = 32767;        /* max width */
  54.     
  55.     newSize = GrowWindow(theWindow,gEvent.where,&growLimitSizes);
  56.     newHeight = HiWord(newSize);
  57.     newWidth = LoWord(newSize);
  58.     SizeWindow(theWindow,newWidth,newHeight,TRUE);
  59. }
  60.  
  61. void DoZoom(WindowPtr theWindow, int part)
  62. {
  63.     GrafPtr savePort;
  64.     
  65.     GetPort(&savePort);
  66.     SetPort(theWindow);
  67.     
  68.     if(TrackBox(theWindow,gEvent.where,part)) {
  69.         ZoomWindow(theWindow,part,true);
  70.     }
  71.     
  72.     SetPort(savePort);
  73. }
  74.  
  75.  
  76.  
  77. void DoMenu(long msel)
  78. {
  79.     int item,menu;
  80.     item = msel;
  81.     menu = msel >> kLow16Bits;
  82.     
  83.     MenuDispatch(menu, item);
  84.     
  85.     HiliteMenu(0);                        /* remove menu title hiliting */
  86. }
  87.  
  88. void DoKey(void)
  89. {
  90.     WindowPtr    fWindow;
  91.         
  92.     if((gEvent.modifiers & cmdKey) == FALSE) {
  93.         fWindow = FrontWindow();
  94.         if( fWindow != nil)
  95.             KeyGameWindow(fWindow,GetWRefCon(fWindow));
  96.     } else {
  97.         AdjustMenus();
  98.         DoMenu(MenuKey(gEvent.message & charCodeMask));    
  99.     }
  100. }
  101.  
  102.  
  103. void DoUpdate(void)
  104. {
  105.     WindowPtr    updateWindow;
  106.     GrafPtr        savePort;
  107.     
  108.     
  109.     GetPort(&savePort);                        /* save current port */
  110.     
  111.     updateWindow=(WindowPtr)gEvent.message;    /* get windowPtr from event msg */
  112.     SetPort(updateWindow);
  113.     
  114.     BeginUpdate(updateWindow);                        
  115.  
  116.     UpdateGameWindow(updateWindow,GetWRefCon(updateWindow));
  117.     
  118.     DrawControls(updateWindow);                /* draw any controls in the window */
  119.  
  120.     EndUpdate(updateWindow);
  121.         
  122.     SetPort(savePort);
  123. }
  124.  
  125.  
  126. void ActivateWindow(WindowRecord    *newFrontWindow)
  127. {
  128.     /* This window is now active.  Controls should be enabled, etc. */
  129. }
  130.  
  131. void DeactivateWindow(WindowRecord    *newBehindWindow)
  132. {
  133.     /* 
  134.         do anyting necessary to deactivate your windows here.
  135.         controls should be dimmed, etc.
  136.     */
  137. }
  138.  
  139. void DoActivate(void)
  140. {
  141.     if(gEvent.modifiers & activeFlag)
  142.         ActivateWindow((WindowRecord *)gEvent.message);
  143.     else
  144.         DeactivateWindow((WindowRecord *)gEvent.message);
  145. }
  146.  
  147. void DoOSEvent(void)
  148. {
  149.     if( (gEvent.message >> kLow24Bits) == suspendResumeMessage)
  150.         gBackgroundFlag = !(gEvent.message & resumeFlag);
  151. }
  152.  
  153. void DoClick(void)
  154. {
  155.     WindowPtr    theWindow;
  156.     short        part;
  157.     
  158.     part = FindWindow(gEvent.where, &theWindow);
  159.     
  160.     switch(part) {
  161.         case inDesk:        
  162.         break;
  163.         case inMenuBar:        AdjustMenus();
  164.                             DoMenu(MenuSelect(gEvent.where));
  165.         break;
  166.         case inSysWindow:    SystemClick(&gEvent,theWindow);
  167.         break;
  168.         case inContent:        DoClickInContent(theWindow);
  169.         break;
  170.         case inDrag:        DoDragWindow(theWindow);
  171.         break;
  172.         case inGrow:        DoGrowWindow(theWindow);
  173.         break;
  174.         case inGoAway:        DoCloseWindow(theWindow);
  175.         break;
  176.         case inZoomIn:
  177.         case inZoomOut:        DoZoom(theWindow,part);
  178.         break;
  179.         default:            
  180.         break;
  181.     }
  182. }
  183.  
  184.  
  185. void MainEvent(void)
  186. {
  187.  
  188.     if(WaitNextEvent(everyEvent,&gEvent,0,nil)) {
  189.         switch(gEvent.what) {
  190.             case nullEvent:                
  191.             break;
  192.             case mouseDown:            DoClick();            
  193.             break;
  194.             case mouseUp:                                         
  195.             break;
  196.             case keyDown:            DoKey();                
  197.             break;
  198.             case keyUp:                                             
  199.             break;
  200.             case autoKey:            DoKey();                
  201.             break;
  202.             case updateEvt:            DoUpdate();            
  203.             break;
  204.             case diskEvt:                                         
  205.             break;
  206.             case activateEvt:        DoActivate();            
  207.             break;
  208.             case networkEvt:                                    
  209.             break;
  210.             case driverEvt:                                         
  211.             break;
  212.             case app1Evt:                                         
  213.             break;
  214.             case app2Evt:                                         
  215.             break;
  216.             case app3Evt:                                         
  217.             break;
  218.             case osEvt:                DoOSEvent();            
  219.             break;
  220.             case kHighLevelEvent:    DoHighLevelEvent(&gEvent);
  221.             default:                                            
  222.             break;
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228.